// pages/home-Cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkeds:false,
    select:true,
    zongjiaee:0,
    chooseSize:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var height= wx.getSystemInfoSync().windowHeight
    var width=wx.getSystemInfoSync().windowWidth
    console.log(height,width);
    // this.data.value.forEach((res)=>{
    //   console.log(res);
    // })
  },
  //反选
  radio:function(e){
    let index=e.currentTarget.dataset.id
    if(this.data.value[index].Selected==true){
      this.setData({select:false})
      this.data.value[index].Selected=false
      this.setData({value:this.data.value})
    }else{
      this.data.value[index].Selected=true
      this.setData({value:this.data.value})
      var shiyan= this.data.value.every((res)=>{
          return res.Selected == true
        })
        if(shiyan){
          this.setData({select:true})
        }
    }
   
  },
  //全选
  select:function(){
    if(this.data.select==true){
     let keyi= this.data.value.map((res)=>{
         res.Selected=false
         return res
     })
     this.setData({value:keyi})
      this.setData({select:false})
    }else{
      let keyi= this.data.value.map((res)=>{
        res.Selected=true
        return res
    })
    this.setData({value:keyi})
      this.setData({select:true})

    }
  },
  jia:function(e){
    let index=e.currentTarget.dataset.index
    let kexi= this.data.value.map((res)=>{
      if(res.id==this.data.value[index].id){
         res.index++
      }
      return res
    })
    wx.setStorageSync('shopping', JSON.stringify(kexi))
    this.setData({value:kexi})
    this.name()

  },
  jian:function(e){
    let index=e.currentTarget.dataset.index
    if(this.data.value[index].index>1){
      let kexi= this.data.value.map((res)=>{
        if(res.id==this.data.value[index].id){
           res.index--
        }
        return res
      })
      wx.setStorageSync('shopping', JSON.stringify(kexi))
      this.setData({value:kexi})
      this.name()
    }else{
      wx.showToast({
        title: '商品不能再少啦',
      })
      
    }
  },
  name:function () {
    let bb= this.data.value.map((res)=>{
      return res.Price * res.index
    })
    var jijian=0
    this.data.value.forEach((res)=>{
      jijian+=res.index
    })
    this.setData({jijian:jijian})
    var  zongjiaee=0
    bb.forEach((res)=>{
      zongjiaee+=res
    })
    let keyiwu= zongjiaee.toFixed(2)
    this.setData({zongjiaee:keyiwu})
  },
  genduo:function (e) {
    let id=e.currentTarget.dataset.id
    var that=this
    wx.showModal({
      title: '您确定删除该商品吗',
      content: '删除该商品',
      confirmColor:'#fd1f02',
      success (res) {
        if (res.confirm) {
         let aa= that.data.value.splice(id,1)
         console.log(aa);
         wx.setStorageSync('shopping', JSON.stringify(that.data.value))
         that.setData({value:that.data.value})
         that.name()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  mairu:function () {
    wx.showToast({
      title: '此功能还未开发',
    })
  },
  //向上弹出
  showshadow:function () {
    if(this.data.chooseSize==false){
      this.setData({chooseSize:true})
      this.chooseSezi()
    }else{
      this.hideModal()
      this.setData({chooseSize:false})

    }
  },
  chooseSezi: function (e) {
    // 用that取代this，防止不必要的情况发生
    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 500,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(1000).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      chooseSize: true
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动 滑动时间
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),
        clearcart: false
      })
    }, 100)
  },
  hideModal: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(700).step()
    that.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),
        chooseSize: false
      })
    }, 500)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    try {
      var value = JSON.parse(wx.getStorageSync('shopping'))
      if (value) {
        this.setData({value:value})
      }
    } catch (e) {
      // Do something when catch error
      
    }

    let bb= this.data.value.map((res)=>{
      return res.Price * res.index
    })
    var jijian=0
    this.data.value.forEach((res)=>{
      jijian+=res.index
    })
    this.setData({jijian:jijian})
    var  zongjiaee=0
    bb.forEach((res)=>{
      zongjiaee+=res
    })
    let keyiwu= zongjiaee.toFixed(2)
    this.setData({zongjiaee:keyiwu})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})